package com.wwe.kotlinstudy.util

/**
 * @name KotlinStudy
 * @package name：com.wwe.kotlinstudy.util
 * @anthor DavidZhang
 * @time 2018/4/17 11:13
 * @class describe
 */
class ArrayStudy {

    fun test1() {
        val arrs = arrayOf(1,2,3) //创建了array [1, 2, 3]

        //遍历数组
        for(arr in arrs) {

        }

        //遍历数组下标
        for(arr in arrs.indices) {

        }
    }

    /**创建一个 Array 初始化为 ["0", "1", "4", "9", "16"]*/
    fun test2() {
        var arr2 = Array(5,{i ->
            i*i
        })
    }

    /**创建一个指定大小、元素都为空的数组*/
    fun test3() {
        arrayOfNulls<Int>(3)
    }

    /**Kotlin 也有无装箱开销的专门的类来表示原生类型数组
     * 例如ByteArray，ShortArray，IntArray*/
    fun test4() {
        val x: IntArray = intArrayOf(1, 2, 3)
    }

    fun useFor() {

        val students: Array<String> = arrayOf("YY","Kugou","FM","Igola")

        for((index,student) in students.withIndex()) {

            println("The element at $index is $student")
        }
    }

}